package Operations.Calculate;

import Data.DbData;
import GUI.ResultData;
import java.util.ArrayList;

/**
 * @author Martin
 * @version 1.0
 * @created 24-III-2012 8:05:00
 */
public class Average extends ACalculate {

    public Average(ArrayList<DbData> data,ACalculate next,ResultData result){  
        super(data,next,result);
    }
        
    @Override
    public void doOperation(){
        double sum = 0;
        int count = 0;
        
        for(DbData row : dbData)
        {
            count++;
            sum = sum + row.getFlow();                    
        }
        if(count>0)
            this.resultData.setAverage(sum/count);
        else
            this.resultData.setAverage(0);
        super.doOperation();
    }

    @Override
    public void run() {
        doOperation();
    }

}